package ru.gelin.android.bells;

import static ru.gelin.android.bells.Utils.isTomorrow;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ru.gelin.android.bells.timers.Timer;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 *  Adapter for collection of timers.
 */
public class TimersListAdapter extends BaseAdapter {

    /** Application context */
    Context context;
    /** Collection of timers */
    TimerStorage timers;
    /** List of timers (to map positions and IDs) */
    List<Timer> timersList = new ArrayList<Timer>();
    /** Current time (when the activity starts) */
    Date now;
    
    public TimersListAdapter(Context context, TimerStorage timers, Date now) {
        this.context = context;
        this.timers = timers;
        this.now = now;
        timersList = timers.select(now);
    }
    
    @Override
    public int getCount() {
        return timers.size();
    }

    @Override
    public Object getItem(int position) {
        return timersList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return timersList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.timers_list_item, parent, false);
        }
        final Timer timer = timersList.get(position);
        Resources resources = context.getResources();
        TextView timerName = (TextView)view.findViewById(R.id.timer_name);
        timerName.setText(getTimerName(resources, timer));
        TextView timerPeriod = (TextView)view.findViewById(R.id.timer_period);
        timerPeriod.setText(resources.getString(
                R.string.list_timer_period, 
                timer.getPeriod().getHours(), timer.getPeriod().getMinutes()));
        TextView timerAlarm = (TextView)view.findViewById(R.id.timer_alarm);
        Date alarmTime = timer.getAlarmTime(now);
        timerAlarm.setText(resources.getString(
                isTomorrow(now, alarmTime) ? 
                        R.string.timer_alarm_time_tomorrow : R.string.timer_alarm_time, 
                alarmTime));
        
        CheckBox enabledCheckBox = (CheckBox)view.findViewById(R.id.timer_enabled);
        enabledCheckBox.setChecked(timer.isEnabled());
        enabledCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                enableTimer(timer, isChecked);
            }
        });
        
        return view;
    }
    
    /**
     *  Makes timer name. It is the message if the message is set,
     *  or the period and next start time if the message is not set.
     */
    String getTimerName(Resources resources, Timer timer) {
        String name = timer.getMessage();
        if (name == null || "".equals(name)) {
            name = resources.getString(
                    R.string.list_default_timer_name, 
                    timer.getPeriod().getHours(), timer.getPeriod().getMinutes(),
                    timer.getStartTime());
        }
        return name;
    }
    
    void enableTimer(Timer timer, boolean isChecked) {
        timer.setEnabled(isChecked);
        timers.update(timer);
        timers.save();
    }
    
    @Override
    public boolean hasStableIds() {
        return true;
    }

}
